'use strict';

const assert = require('assert');

module.exports = path => async ctx => {
  assert(path.startsWith('/'), 'path必须是/开头');

  const params = Object.assign({}, ctx.params, ctx.query, ctx.body);
  const host = ctx.app.config.GATEWAY_HOST;
  ctx.set('yc-delegate-path', host + path);
  if (ctx.method === 'GET') {
    const data = await ctx.ycGet(path, params);
    ctx.success({ data });
  }
  if (ctx.method === 'POST') {
    const data = await ctx.ycPost(path, params);
    ctx.success({ data });
  }
};
